﻿using Newtonsoft.Json;
using Omabar.BLL;
using Omabar.Common;
using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace Omabar.CMS.Handler
{
    /// <summary>
    /// RouteAdd 的摘要说明
    /// </summary>
    public class TailorAdd : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            RESP_Info info = new RESP_Info();
            try
            {
                context.Response.ContentType = "text/plain";
                string TAILOR_NAME = context.Request["TAILOR_NAME"];
                string TAILOR_QQ = context.Request["TAILOR_QQ"];
                string TAILOR_PHONE = context.Request["TAILOR_PHONE"];
                string TAILOR_INFO = context.Request["TAILOR_INFO"];
                string TAILOR_SPECIALTY = context.Request["TAILOR_SPECIALTY"];
                string TAILOR_CASE = context.Request["TAILOR_CASE"];
                string TAILOR_HEAD_IMG = context.Request["TAILOR_HEAD_IMG"];
                string TAILOR_HOT = context.Request["TAILOR_HOT"];
                string[] photos = string.IsNullOrEmpty(context.Request["photo[0][]"]) ? new string[] { } : context.Request["photo[0][]"].Split(',');

                Tailor_BLL tailorBLL = new Tailor_BLL();
                Tailor tailor = new Tailor()
                {
                    TAILOR_ID = Util.GetUID(),
                    TAILOR_NAME = TAILOR_NAME,
                    TAILOR_INFO = TAILOR_INFO,
                    TAILOR_SPECIALTY = TAILOR_SPECIALTY,
                    TAILOR_CASE = TAILOR_CASE,
                    TAILOR_QQ = TAILOR_QQ,
                    TAILOR_PHONE = TAILOR_PHONE,
                    TAILOR_HEAD_IMG = TAILOR_HEAD_IMG,
                    TAILOR_HOT = int.Parse(TAILOR_HOT)
                };

                //添加定制师图片
                Tailor_IMG_BLL rgBLL = new Tailor_IMG_BLL();
                foreach (string photo in photos)
                {
                    rgBLL.Add(new Tailor_IMG()
                    {
                        Tailor_ID = tailor.TAILOR_ID,
                        PHOTO_ID = photo
                    });

                    System.Threading.Thread.Sleep(123);
                }

                info = tailorBLL.Add(tailor);
                System.Threading.Thread.Sleep(123);
            }
            catch (Exception ex)
            {
                Tom.Net.Log.LogManager.Error("添加定制师失败", ex);
                info.Success = false;
                info.Title = "错误";
                info.Tip = "<h5><p>添加定制师失败</p></h5>";
                info.Url = "TailorAdd.aspx";
            }

            context.Response.Write(JsonConvert.SerializeObject(info));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}